<template>
	<view v-if="goodsinfo.goods_name" class="goodsif">
		<!-- 轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item,index) in goodsinfo.pics" :key="item.pics_id">
				<image :src="item.pics_big" style="width: 100%;height: 100%;" @click="hadoyulan(index)"></image>
			</swiper-item>
		</swiper>
		<!-- 商品信息区域 -->
		<view class="shangp">
			<view class="paci">{{goodsinfo.goods_price | RMBformat}}</view>
			<view class="totao">
				<view class="titos">{{goodsinfo.goods_name}}</view>
				<view class="shouc">
					<uni-icons type="star" size="25" color="gray" />
					<view class="shcya">收藏</view>
				</view>
			</view>
			<view class="bottm">快递: 免运费</view>
		</view>
		<!-- 底部商品详情页面 -->
		<rich-text :nodes="goodsinfo.goods_introduce" />
		<!-- 底部商品导航区域 -->
		<view class="goods_nav">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="bottonClick"/>
		</view>
	</view>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
	export default {
		data() {
			return {
				goodsinfo: {},
				imgurl: [],
				options: [{
					icon: 'shop',
					text: '店铺',
					infoColor: "red"
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
				  text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			};
		},
		onLoad(options) {
			//获取商品详情数据
			this.gerGoodsDetail(options.id)
		},
		computed:{
			...mapGetters(['total'])
		},
		mounted(){
		},
		methods: {
			//底部商品导航加入/立即购买点击事件
			bottonClick(e){
				if(e.content.text === '加入购物车'){
					const goods = {
						goods_id:this.goodsinfo.goods_id,
						goods_name:this.goodsinfo.goods_name,
						goods_price:this.goodsinfo.goods_price,
						goods_count:1,
						goods_small_logo:this.goodsinfo.goods_small_logo,
						goods_state:true
					}
					this.$store.commit('addToCart',goods)
				}
			},
			//底部商品导航店铺/购物车点击事件
			onClick(e){
				if(e.content.text === '购物车'){
					uni.switchTab({
						url:'/pages/cart/cart'
					})
				}
			},
			//获取商品数据的详情
			async gerGoodsDetail(id) {
				let {
					data: res
				} = await uni.$http.get('/goods/detail', {
					goods_id: id
				})
				if (res.meta.status === 200) {
					res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g,
						'<img style="display:block;"').replace(/webp /g, 'jpg')
					this.goodsinfo = res.message
				} else {
					uni.$showMsg()
				}
			},
			//预览图片
			hadoyulan(index) {
				uni.previewImage({
					current: index,
					urls: this.goodsinfo.pics.map(item => {
						return item.pics_big
					})
				})
			}
		},
		watch:{
			total:{
				handler(newVal){
					const objct = this.options.find(item=>item.text === '购物车')
					if(objct){
						objct.info = newVal
					}
				},
				immediate:true
			}
		}
	}
</script>

<style lang="scss" scoped>
	.goodsif{
		padding-bottom: 50px;
	}
	.goods_nav{
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
	swiper {
		height: 750rpx;
	}

	.shangp {
		border-top: 1px solid #afafaf;
		padding: 10px;
		padding-right: 0;
		background: #ffffff;

		.paci {
			font-size: 25px;
			font-weight: 200;
			color: #DD524D;
		}

		.totao {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.titos {
				font-size: 15px;
				width: 305px;
				height: 43px;
				margin-top: 7px;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				overflow: hidden;
			}

			.shouc {
				display: flex;
				flex-direction: column;
				align-items: center;
				width: 55px;
				height: 50px;
				border-left: 1px solid #d9d9d9;

				.shcya {
					font-size: 15px;
					color: #949494;
				}
			}
		}
	
	.bottm {
			margin-top: 10px;
			font-size: 15px;
			color: #949494;
		}
	}
</style>
